package com.lxm.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxm.common.contants.ItemConstants;
import com.lxm.dto.Result;
import com.lxm.entity.ItemType;
import com.lxm.mapper.ItemTypeDao;
import com.lxm.service.ItemTypeService;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class ItemTypeServiceImpl extends ServiceImpl<ItemTypeDao, ItemType> implements ItemTypeService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result addItemType(String TypeName) {
        ItemType itemType = new ItemType();
        itemType.setName(TypeName);
        boolean isSave = save(itemType);
        if (!isSave) return Result.fail(ItemConstants.ITEM_TYPE_ADD_FAIL);
        stringRedisTemplate.opsForHash().put(ItemConstants.ITEM_TYPE_KEY,
                itemType.getId().toString(), TypeName);
        return Result.ok();
    }

    @Override
    public Result deleteItemType(Long id) {
        boolean isRemove = removeById(id);
        if (!isRemove) return Result.fail(ItemConstants.ITEM_TYPE_DELETE_FAIL);
        stringRedisTemplate.opsForHash().delete(ItemConstants.ITEM_TYPE_KEY,
                id.toString());
        return Result.ok();
    }

    @Override
    public Result updateItemType(Long id, String typeName) {
        ItemType itemType = new ItemType();
        itemType.setId(id);
        itemType.setName(typeName);
        boolean isUpdate = updateById(itemType);
        if (!isUpdate) return Result.fail(ItemConstants.ITEM_TYPE_UPDATE_FAIL);
        stringRedisTemplate.opsForHash().put(ItemConstants.ITEM_TYPE_KEY,
                id.toString(), typeName);
        return Result.ok();
    }
}